package com.itheima.web.controller;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import com.itheima.utils.QiNiuUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.print.attribute.standard.MediaSize;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {
    ///orderSetting/uploadXLSX
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * excel批量导入预约设置
     * @param multipartFile
     * @return
     */
    @RequestMapping("/uploadXLSX")
    public Result addByXLSX(@RequestParam("excelFile") MultipartFile multipartFile) {

        //System.out.println("到达了后端");
        try {
            List<String[]> strings = POIUtils.readExcel(multipartFile);
            //将集合传递到service层进行解析和存储
            orderSettingService.addByXlsx(strings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 获取order setting预约信息（跟随月份更改数据显示）
     * @return
     */
    @RequestMapping("/searchOrderSetting")
    public Result searchOrderSetting(String date) {

        //查询所得
        List<Map<String,Object>> list = orderSettingService.searchOrderSettingByMonth(date);
        //查询当月预约数据成功返回一个list集合数据,其中封装order Setting对象
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
    }

    @RequestMapping("/settingOrderNumber")
    public Result settingOrderNumber(@RequestBody OrderSetting orderSetting){

        try {
            orderSettingService.settingOrderNumber(orderSetting);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}